package com.amazoon;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class BookStoreMain {

		public final static String USERNAME = "user1";
		public final static char[] PASS = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd', '1'};
	
	public static void main(String[] args) {

		final JFrame frame = createMainFrame();
		final JPanel p1 = createPanel(frame);

		final JLabel usernameLbl = new JLabel();
		usernameLbl.setText("Username");
		p1.add(usernameLbl);

		final JTextField usernameTxtFld = new JTextField(20);
		p1.add(usernameTxtFld);
				
		final JLabel password = new JLabel();
		password.setText("Password");
		p1.add(password);

		final JPasswordField passwordFld = new JPasswordField(20);
		p1.add(passwordFld);
		
		JButton b1 = new JButton("Submit");
		b1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (usernameTxtFld.getText().equals(USERNAME) && Arrays.equals(passwordFld.getPassword(), PASS)) {
					p1.removeAll();
					p1.setVisible(false);
					
					final JLabel welcomeLbl = new JLabel();
					welcomeLbl.setText("Welcome " + usernameTxtFld.getText());
					p1.add(welcomeLbl);
					p1.setVisible(true);
					frame.setVisible(true);
				}				

				else JOptionPane.showMessageDialog(null, "Incorrect Username / password");
			}
		});
		
		p1.add(b1);
		frame.setVisible(true);
	}

	private static JFrame createMainFrame() {
		final JFrame frame = new JFrame("Welcome to Amazoon BookStore");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(350, 150);
		return frame;
	}

	private static JPanel createPanel(final JFrame frame) {
		final JPanel panel = new JPanel();
		frame.add(panel);
		return panel;
	}
}
